package com.tenement.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.entity.HouseUser;
import com.tenement.mapper.HouseUserMapper;
import com.tenement.service.HouseUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.tenement.entity.table.HouseUserTableDef.HOUSE_USER;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class HouseUserServiceImpl extends ServiceImpl<HouseUserMapper, HouseUser> implements HouseUserService {


    private final HouseUserMapper houseUserMapper;


    @Override
    public long exists(List<Long> houseIds) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(HOUSE_USER).where(HOUSE_USER.HOUSE_ID.in(houseIds));
        return houseUserMapper.selectCountByQuery(queryWrapper);
    }

    @Override
    public boolean audit(HouseUser houseUser) {
        return UpdateChain.of(HouseUser.class)
                .set(HOUSE_USER.STATUS, houseUser.getStatus())
                .set(HOUSE_USER.REMARK,houseUser.getRemark())
                .where(HOUSE_USER.USER_ID.eq(houseUser.getUserId()))
                .and(HOUSE_USER.HOUSE_ID.eq(houseUser.getHouseId()))
                .update();
    }

    @Override
    public int add(HouseUser houseUser) {
        return houseUserMapper.insertSelective(houseUser);
    }

    @Override
    public int unbind(HouseUser houseUser) {
        QueryWrapper wrapper = QueryWrapper.create().from(HOUSE_USER)
                .where(HOUSE_USER.USER_ID.eq(houseUser.getUserId()).and(HOUSE_USER.HOUSE_ID.eq(houseUser.getHouseId())));
        return houseUserMapper.deleteByQuery(wrapper);
    }

}
